package com.hu.zhcc.shiro.service.junM.Impl;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.common.entity.PageRequest;
import com.hu.zhcc.shiro.dao.junM.TInterposeClassifyMapper;
import com.hu.zhcc.shiro.dao.lgx.THealthmgrMapper;
import com.hu.zhcc.shiro.dao.lgx.TMemberMapper;
import com.hu.zhcc.shiro.dao.lgx.TMemberMedicalhistoryMapper;
import com.hu.zhcc.shiro.dao.lgx.TSetmealMapper;
import com.hu.zhcc.shiro.dao.xsf.TDiseaseLibraryMapper;
import com.hu.zhcc.shiro.dao.xsf.TDiseaseMapper;
import com.hu.zhcc.shiro.entity.junM.dto.TInterposeClassifyDTO;
import com.hu.zhcc.shiro.entity.junM.pojo.TInterposeClassify;
import com.hu.zhcc.shiro.entity.lgx.THealthmgr;
import com.hu.zhcc.shiro.entity.lgx.TMember;
import com.hu.zhcc.shiro.entity.lgx.TMemberMedicalhistory;
import com.hu.zhcc.shiro.entity.lgx.TSetmeal;
import com.hu.zhcc.shiro.entity.xsf.pojo.TDisease;
import com.hu.zhcc.shiro.entity.xsf.pojo.TDiseaseLibrary;
import com.hu.zhcc.shiro.service.junM.InterposeClassifyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class InterposeClassifyServiceImpl implements InterposeClassifyService {

    @Autowired
    private TInterposeClassifyMapper tInterposeClassifyMapper;
    @Autowired
    private TMemberMedicalhistoryMapper tMemberMedicalhistoryMapper;
    @Autowired
    private TMemberMapper tMemberMapper;
    @Autowired
    private TDiseaseLibraryMapper tDiseaseLibraryMapper;
    @Autowired
    private TSetmealMapper tSetmealMapper;
    @Autowired
    private THealthmgrMapper tHealthmgrMapper;
    @Autowired
    private TDiseaseMapper tDiseaseMapper;

    @Override
    public Page<TInterposeClassifyDTO> queryInterposeClassifyAll(Map<String, Object> paremeters, int offset, int limit) {

        int total = this.tInterposeClassifyMapper.countInterposeClassify(paremeters);
        List<TInterposeClassifyDTO> dtoList = null;
        if(total>0){
            PageRequest pageRequest = new PageRequest(offset, limit, paremeters, null);
            List<TInterposeClassify> list = this.tInterposeClassifyMapper.queryInterposeClassifyAll(pageRequest);
            dtoList = new ArrayList<TInterposeClassifyDTO>(list.size());
            for (TInterposeClassify tInterposeClassify : list) {
                TInterposeClassifyDTO dto = new TInterposeClassifyDTO();
                BeanUtils.copyProperties(tInterposeClassify, dto);
                dtoList.add(dto);
            }
        }
        return new Page<TInterposeClassifyDTO>(total,dtoList);
    }

    @Override
    public TMember getId(Integer id) {
        //病例史
        TMemberMedicalhistory medicalhistorys = this.tMemberMedicalhistoryMapper.selectByMemberId(id);
        //疾病库
        List<TDiseaseLibrary> tDiseaseLibraries = tDiseaseLibraryMapper.queryDiseaseLibraryByMemberId(id);
        TMember tMember = tMemberMapper.selectByPrimaryKey(id);
        tMember.setTMemberMedicalhistory(medicalhistorys);
        tMember.setTDiseaseLibrarys(tDiseaseLibraries);
        return tMember;
    }

    @Override
    public TSetmeal getSetmealById(Integer id) {

        TSetmeal tSetmeal = tSetmealMapper.queryByMemberId(id);

        return tSetmeal;
    }

    @Override
    public List<TDiseaseLibrary> getCheckListInit() {

        return tDiseaseLibraryMapper.queryDiseaseLibearyAll();
    }

    @Override
    public List<THealthmgr> getSelectListInit() {

        System.out.println(tHealthmgrMapper.queryHealthmgrAll());

        return tHealthmgrMapper.queryHealthmgrAll();
    }

    @Override
    public void updateDiseaseLibrary(Integer id, List<String> diseaseIds, String healthName) {
        int i1 = tDiseaseMapper.deleteByMemberId(id);
        System.out.println(i1);
        String diseases="";
        for(int i=0;i<diseaseIds.size();i++){
            TDisease tDisease = new TDisease();
            tDisease.setMemberId(id);
            tDisease.setDiseaseLibraryLib(Integer.valueOf(diseaseIds.get(i)));
            tDisease.setWho(0);
            tDiseaseMapper.insertSelective(tDisease);
            TDiseaseLibrary tDiseaseLibrary = this.tDiseaseLibraryMapper.selectByPrimaryKey(Integer.valueOf(diseaseIds.get(i)));
            diseases+=tDiseaseLibrary.getName()+",";
        }
        TInterposeClassify tInterposeClassify = new TInterposeClassify();
        tInterposeClassify.setTMemberId(id);
        tInterposeClassify.setHealthmgrId(Integer.valueOf(healthName));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
        tInterposeClassify.setUpdatetime(simpleDateFormat.format(new Date()));
        tInterposeClassify.setStatus(1);
        tInterposeClassify.setPeopleClassify(diseases.length()>0?diseases.substring(0,diseases.length()-1):diseases);
        this.tInterposeClassifyMapper.updateByMemberId(tInterposeClassify);

//        TInterposeClassify tInterposeClassify1 = this.tInterposeClassifyMapper.selectByMemberId(id);


    }
}
